package reduce.fz.com.reduce.util.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.Stack;

import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ReduceLog;
import reduce.fz.com.reduce.util.ScreenUtils;

/**
 * FeelApp
 * 提供Activity的管理，避免推出应用造成关闭不了某些隐藏的activity造成的问题
 * @author lipf
 */
public class ReduceApplication extends Application {

    private static Stack<Activity> activityStack;
    private static ReduceApplication mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        ScreenUtils.initScreen(this);
        HttpUtils.init(this);
    }

    public static ReduceApplication getInstance() {
        return mInstance;
    }

    /**
     * 获取activity堆栈大小
     */
    public int getActivityStackSize(){
        if(activityStack ==null){
            activityStack = new Stack<Activity>();
        }
        return activityStack.size();
    }
    /**
     * add Activity 添加Activity到栈
     */
    public void addActivity(Activity activity){
        if(activityStack ==null){
            activityStack =new Stack<Activity>();
        }
        activityStack.add(activity);
    }
    /**
     * get current Activity 获取当前Activity（栈中最后一个压入的）
     */
    public Activity currentActivity() {
        if(activityStack==null || activityStack.isEmpty()){
            return null;
        }
        Activity activity = activityStack.lastElement();
        return activity;
    }
    /**
     * 栈 中是否有 Activity
     */
    public boolean hasActivity(Class<?> cls) {
        if( cls == null || activityStack == null || activityStack.isEmpty() ){
            return false;
        }
        for(Activity activity1 : activityStack ){
            if( cls.equals(activity1.getClass()) ){
                return true;
            }
        }
        return false;
    }
    /**
     * 结束当前Activity（栈中最后一个压入的）
     */
    public void finishActivity() {
        if(activityStack==null || activityStack.isEmpty()){
            return ;
        }
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            if(activityStack!=null){
                activityStack.remove(activity);
            }
            if( !activity.isFinishing() ){
                activity.finish();
            }
            activity = null;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        if(activityStack!=null) {
            for (int i = activityStack.size()-1; i >= 0; i--) {
                Activity activity = activityStack.get(i);
                if (activity.getClass().equals(cls)) {
                    finishActivity(activity);
                }
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if(activityStack!=null) {
            for (int i = 0, size = activityStack.size(); i < size; i++) {
                if (null != activityStack.get(i)) {
                    try{
                        Activity activity = activityStack.get(i);
                        if( !activity.isFinishing() ){
                            activity.finish();
                        }
                    }catch (Exception e){
                        ReduceLog.e(e);
                    }
                }
            }
            activityStack.clear();
        }
    }

    @Override
    public void onTerminate() {
        try{
            exitApp();
            super.onTerminate();
        }catch (Exception e){
            ReduceLog.e(e);
        }
    }

    /**
     * 退出应用程序
     */
    public void exitApp() {
        try {
            finishAllActivity();
        } catch (Throwable e) {
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }
}
